import base64, json, hashlib

def create_cookie(username):
    # 头部
    data = {'typ': 'JWT', 'alg': 'HS256'}
    header = base64.b64encode(json.dumps(data).encode()).decode()

    # 载荷
    data = {
        "sub": "1234567890",
        "exp": "3422335555",
        "name": username,
        "admin": True,
        "info": "232323ssdgerere3335dssss"
    }
    preload = base64.b64encode(json.dumps(data).encode()).decode()

    # 签证
    # from django.conf import settings
    # secret = settings.SECRET_KEY
    secret = '7#s=eh&=lrna8c1ulh(_fmu2q7ert4fh994_t!#2e7$iauhpp('
    sign = f"{header}.{preload}.{secret}"

    hs256 = hashlib.sha256()
    hs256.update(sign.encode())
    signature = hs256.hexdigest()

    jwt = f"f{header}.{preload}.{signature}"
    print(jwt)

    return jwt